// Creates a namespace for your Project
YAHOO.namespace("EUKO");

// declare the module
YAHOO.EUKO.getFixtures = function () {
    var fixturesTable;
    var callback;
    var lang;
    var downloadCalBaseURL;
    var subscribeCalBaseURL;
    var queryParamValue;
    
    // set some configuation
    var config = [
        {
            listId: "team-list-a",
            qParam: "cc"
        },
        
        {
            listId: "team-list-b",
            qParam: "cc"
        },
        
        {
            listId: "team-list-c",
            qParam: "cc"
        },
        
        {
            listId: "team-list-d",
            qParam: "cc"
        },
        
        {
            listId: "stage-list",
            qParam: "stageId"
        },
        
        {
            listId: "city-list",
            qParam: "city"
        },
        
        {
            listId: "dates",
            qParam: "date"
        }
    ];
    
    var configLength = config.length;
    
    var alterDom = function () {
        var len;
        var c;
        var anchor;
        
        // get id="fixtures-container"
        var fixturesContainerEl = document.getElementById("fixtures-container");
        fixturesContainerEl.innerHTML = fixturesTable;
        
        // get all elements with class download
        var downloadLiEls = YAHOO.util.Dom.getElementsByClassName("download", "li");
        len = downloadLiEls.length;
        
        for (c = 0; c < len; c++) {
            // get anchor within
            anchor = downloadLiEls[c].getElementsByTagName("a")[0];
            anchor.href = downloadCalBaseURL + window.location.href + queryParamValue;
        }
        
        // get all elements with class download
        var subscribeLiEls = YAHOO.util.Dom.getElementsByClassName("subscribe", "li");
        len = subscribeLiEls.length;
        
        for (c = 0; c < len; c++) {
            // get anchor within
            anchor = subscribeLiEls[c].getElementsByTagName("a")[0];
            anchor.href = subscribeCalBaseURL + window.location.href + queryParamValue;
        }
    };
    
    var AjaxObject = {
    
        handleSuccess: function (o) {
            // This member handles the success response
            // and passes the response object o to AjaxObject's
            // processResult member.
            this.processResult(o);
        },
    
        handleFailure: function (o) {
            console.log("call to get fixtures failed");
        },
    
        processResult: function (o) {
            //alert(o.responseText);
            //fixturesTable = eval("(" + o.responseText + ")");
            //alert(fixturesTable);
            fixturesTable = o.responseText;
            alterDom();
        },
    
        startRequest: function (uri) {
            YAHOO.util.Connect.asyncRequest('GET', uri, callback);
        }
    
    };
    
    callback = {
        success: AjaxObject.handleSuccess,
        failure: AjaxObject.handleFailure,
        scope: AjaxObject
    };
    
    var getFixtures = function (e) {
        YAHOO.util.Event.preventDefault(e);
        
        var queryParam;
        var target = YAHOO.util.Event.getTarget(e);
        
        queryParamValue = target.id;
        var listId = target.parentNode.parentNode.id;
        
        // Dirty Hack for date selector
        if (!listId) {
            listId = "dates";   
        }
        
        for (var i = 0; i < configLength; i++) {
            if (listId === config[i].listId) {
                queryParam = config[i].qParam;
            }
        }
        
        // another Dirty Hack for date selector
        if (listId == "dates") {
            queryParamValue = queryParamValue.split("_")[1];   
        }
        

        AjaxObject.startRequest("/lib/getFixtures.php?" + queryParam + "=" + queryParamValue + "&lang=" + lang);
    };
    
    var init = function (language, downloadCalBase, subscribeCalBase) {
        lang = language;
        downloadCalBaseURL = downloadCalBase;
        subscribeCalBaseURL = subscribeCalBase;
        
        for (var i = 0; i < configLength; i++) {
            var listEl = document.getElementById(config[i].listId);
            if (listEl) {
                var anchors = listEl.getElementsByTagName("a");
                var nbr_anchors = anchors.length;
                        
                for (var y = 0; y < nbr_anchors; y++) {
                    // on click call function to retrieve fixture via ajax
                    YAHOO.util.Event.addListener(anchors[y], "click", getFixtures);
                }
            }
        }
    };

    return {
        // set up which functions should be public
        init: init
    };
}(); 